package com.jstyle.beatecg.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.jstyle.beatecg.R;
import com.jstyle.beatecg.util.ScreenUtils;

/**
 * Created by Administrator on 2018/5/30.
 */

public class EcgDashboardView extends View {
    private Bitmap dashBoardBitmap;
    private Bitmap pointerBitmap;
    private int dashBoardWidth,dashBoardHeight;
    private int pointerWidth,pointerHeight;
    private int defaultWidth;
    private int defaultHeight;
    private int radius;
    private int cicleRadius;
    private int pointerRadius;
    private static float angel=180;
    private Paint mPaint;
    private float mCenterX;
    private float mCenterY;
    private Path path=new Path();
    private static int data;
    private Paint mTextPaint;
    public EcgDashboardView(Context context) {
        super(context);
    }

    public EcgDashboardView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
            height = defaultHeight;
        }
        if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
            width = defaultWidth;
        }
        setMeasuredDimension(width, height);
    }

    private void init(Context context) {
        defaultHeight = ScreenUtils.dip2px(context, 200);
        defaultWidth = ScreenUtils.getScreenWidth(context);

        dashBoardBitmap= BitmapFactory.decodeResource(context.getResources(),R.drawable.dashboard);
        pointerBitmap=BitmapFactory.decodeResource(context.getResources(),R.drawable.pointer);
        dashBoardWidth=dashBoardBitmap.getWidth();
        dashBoardHeight=dashBoardBitmap.getHeight();
        pointerWidth=pointerBitmap.getWidth();
        pointerHeight=pointerBitmap.getHeight();
        radius=ScreenUtils.dip2px(context,30);
        cicleRadius=ScreenUtils.dip2px(context,5);
        pointerRadius=ScreenUtils.dip2px(context,70);
        mPaint=new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.parseColor("#05a0ff"));
        mPaint.setStrokeWidth(ScreenUtils.dip2px(context,3));
        mPaint.setStyle(Paint.Style.STROKE);
        //mPaint.setTextSize(ScreenUtils.dip2px(context,15));

        mTextPaint=new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.BLACK);
        //  mTextPaint.setStrokeWidth(ScreenUtils.dip2px(context,4));
        mTextPaint.setStyle(Paint.Style.STROKE);
        mTextPaint.setTextSize(ScreenUtils.dip2px(context,40));
    }

    private static final String TAG = "EcgDashboardView";
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mCenterX=getWidth()/2;

        int left=getWidth()/2-dashBoardWidth/2;
        int top=0;

        canvas.drawBitmap(dashBoardBitmap,left,top,mPaint);
        // canvas.save();
        // canvas.rotate(angel,getWidth()/2,dashBoardHeight);
        // Bitmap bitmap=rotateBitmap(pointerBitmap,angel);

        // Matrix matrix=new Matrix();
        //  matrix.setRotate(angel);
        // canvas.drawBitmap(pointerBitmap,leftPointer,topPointer,mPaint);
        Bitmap bitmap=rotateBitmap(pointerBitmap,angel);
        int leftPointer=getWidth()/2-bitmap.getWidth()/2;
        int topPointer=dashBoardHeight-radius/2;
        mCenterY=dashBoardHeight;

        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWidth()/2,mCenterY,radius,mPaint);
        drawPointer(canvas);

        Rect rect = new Rect();
        String info=String.valueOf(data);
        mTextPaint.getTextBounds(info, 0, info.length(), rect);
        int width = rect.width();//字体宽度
        int height = rect.height();
        Paint.FontMetrics fm = mTextPaint.getFontMetrics();
        float y = height / 2 - fm.descent + (fm.descent - fm.ascent) / 2;
        canvas.drawText(String.valueOf(info),mCenterX-width/2,mCenterY-pointerRadius,mTextPaint);


    }
    private void drawPointer(Canvas canvas) {
        mPaint.setStyle(Paint.Style.FILL);
        //   mPaint.setColor(mTextColor);
        path.reset();
        float[] point1 = getCoordinatePoint(cicleRadius/2, angel + 90);
        path.moveTo(point1[0], point1[1]);
        float[] point2 = getCoordinatePoint(cicleRadius/2 , angel - 90);
        path.lineTo(point2[0], point2[1]);
        float[] point3 = getCoordinatePoint(pointerRadius, angel);
        path.lineTo(point3[0], point3[1]);
        path.close();
        canvas.drawPath(path, mPaint);
        // 绘制三角形指针底部的圆弧效果

        canvas.drawCircle((point1[0] + point2[0]) / 2, (point1[1] + point2[1]) / 2,
                cicleRadius/2, mPaint);
    }

    public float[] getCoordinatePoint(int radius, float cirAngle) {
        float[] point = new float[2];
        Log.i(TAG, "getCoordinatePoint: "+radius+" "+angel);
        double arcAngle = Math.toRadians(cirAngle); //将角度转换为弧度
        if (cirAngle < 90) {
            point[0] = (float) (mCenterX + Math.cos(arcAngle) * radius);
            point[1] = (float) (mCenterY + Math.sin(arcAngle) * radius);
        } else if (cirAngle == 90) {
            point[0] = mCenterX;
            point[1] = mCenterY + radius;
        } else if (cirAngle > 90 && cirAngle < 180) {
            arcAngle = Math.PI * (180 - cirAngle) / 180.0;
            point[0] = (float) (mCenterX - Math.cos(arcAngle) * radius);
            point[1] = (float) (mCenterY + Math.sin(arcAngle) * radius);
        } else if (cirAngle == 180) {
            point[0] = mCenterX - radius;
            point[1] = mCenterY;
        } else if (cirAngle > 180 && cirAngle < 270) {
            arcAngle = Math.PI * (cirAngle - 180) / 180.0;
            point[0] = (float) (mCenterX - Math.cos(arcAngle) * radius);
            point[1] = (float) (mCenterY - Math.sin(arcAngle) * radius);
        } else if (cirAngle == 270) {
            point[0] = mCenterX;
            point[1] = mCenterY - radius;
        } else {
            arcAngle = Math.PI * (360 - cirAngle) / 180.0;
            point[0] = (float) (mCenterX + Math.cos(arcAngle) * radius);
            point[1] = (float) (mCenterY - Math.sin(arcAngle) * radius);
        }

        return point;
    }

    private Bitmap rotateBitmap(Bitmap origin, float alpha) {
        if (origin == null) {
            return null;
        }
        int width = origin.getWidth();
        int height = origin.getHeight();
        Matrix matrix = new Matrix();
        matrix.setRotate(alpha);
        // 围绕原地进行旋转
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        if (newBM.equals(origin)) {
            return newBM;
        }
        //  origin.recycle();
        return newBM;
    }

    public  void updateData(float angels){
        data= (int) angels;
        angel=(angels-40)*1.5f+180;
      postInvalidate();

    }
}
